package com.example.klinemqflink.utils;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoField;

public class TimeCeilingUtil {


    public static long getCeilingTimestamp(long timestamp, String frequency) {
        switch (frequency) {
            case "1N":
                return getCeilingMinute(timestamp);
            case "5N":
                return getCeiling5Minutes(timestamp);
            case "10N":
                return getCeiling10Minutes(timestamp);
            case "15N":
                return getCeiling15Minutes(timestamp);
            case "30N":
                return getCeiling30Minutes(timestamp);
            case "1H":
                return getCeilingHour(timestamp);
            default:
                throw new IllegalArgumentException("不支持的时间间隔类型: " + frequency);
        }
    }

    // 向上获取整分钟时间戳
    public static long getCeilingMinute(long timestamp) {
        LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.systemDefault());
        if (dateTime.get(ChronoField.SECOND_OF_MINUTE) > 0 || dateTime.get(ChronoField.MILLI_OF_SECOND) > 0) {
            dateTime = dateTime.plusMinutes(1);
        }
        dateTime = dateTime.with(ChronoField.SECOND_OF_MINUTE, 0).with(ChronoField.MILLI_OF_SECOND, 0);
        return dateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    // 向上获取整 5 分钟时间戳
    public static long getCeiling5Minutes(long timestamp) {
        LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.systemDefault());
        int minute = dateTime.getMinute();
        int remainder = minute % 5;
        if (remainder > 0 || dateTime.get(ChronoField.SECOND_OF_MINUTE) > 0 || dateTime.get(ChronoField.MILLI_OF_SECOND) > 0) {
            minute = minute - remainder + 5;
        } else {
            minute = minute - remainder;
        }
        if (minute >= 60) {
            dateTime = dateTime.plusHours(1);
            minute = 0;
        }
        dateTime = dateTime.withMinute(minute).with(ChronoField.SECOND_OF_MINUTE, 0).with(ChronoField.MILLI_OF_SECOND, 0);
        return dateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    // 向上获取整 10 分钟时间戳
    public static long getCeiling10Minutes(long timestamp) {
        LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.systemDefault());
        int minute = dateTime.getMinute();
        int remainder = minute % 10;
        if (remainder > 0 || dateTime.get(ChronoField.SECOND_OF_MINUTE) > 0 || dateTime.get(ChronoField.MILLI_OF_SECOND) > 0) {
            minute = minute - remainder + 10;
        } else {
            minute = minute - remainder;
        }
        if (minute >= 60) {
            dateTime = dateTime.plusHours(1);
            minute = 0;
        }
        dateTime = dateTime.withMinute(minute).with(ChronoField.SECOND_OF_MINUTE, 0).with(ChronoField.MILLI_OF_SECOND, 0);
        return dateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    // 向上获取整 15 分钟时间戳
    public static long getCeiling15Minutes(long timestamp) {
        LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.systemDefault());
        int minute = dateTime.getMinute();
        int remainder = minute % 15;
        if (remainder > 0 || dateTime.get(ChronoField.SECOND_OF_MINUTE) > 0 || dateTime.get(ChronoField.MILLI_OF_SECOND) > 0) {
            minute = minute - remainder + 15;
        } else {
            minute = minute - remainder;
        }
        if (minute >= 60) {
            dateTime = dateTime.plusHours(1);
            minute = 0;
        }
        dateTime = dateTime.withMinute(minute).with(ChronoField.SECOND_OF_MINUTE, 0).with(ChronoField.MILLI_OF_SECOND, 0);
        return dateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    // 向上获取整 30 分钟时间戳
    public static long getCeiling30Minutes(long timestamp) {
        LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.systemDefault());
        int minute = dateTime.getMinute();
        int remainder = minute % 30;
        if (remainder > 0 || dateTime.get(ChronoField.SECOND_OF_MINUTE) > 0 || dateTime.get(ChronoField.MILLI_OF_SECOND) > 0) {
            minute = minute - remainder + 30;
        } else {
            minute = minute - remainder;
        }
        if (minute >= 60) {
            dateTime = dateTime.plusHours(1);
            minute = 0;
        }
        dateTime = dateTime.withMinute(minute).with(ChronoField.SECOND_OF_MINUTE, 0).with(ChronoField.MILLI_OF_SECOND, 0);
        return dateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    // 向上获取整 1 小时时间戳
    public static long getCeilingHour(long timestamp) {
        LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.systemDefault());
        if (dateTime.get(ChronoField.MINUTE_OF_HOUR) > 0 || dateTime.get(ChronoField.SECOND_OF_MINUTE) > 0 || dateTime.get(ChronoField.MILLI_OF_SECOND) > 0) {
            dateTime = dateTime.plusHours(1);
        }
        dateTime = dateTime.withMinute(0).with(ChronoField.SECOND_OF_MINUTE, 0).with(ChronoField.MILLI_OF_SECOND, 0);
        return dateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }
}